<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <canvas id="c" width="500" height="300" style="border: 1px solid #ccc"></canvas>

        <script>
            const cnv = document.getElementById('c')
            const ctx = cnv.getContext('2d')
            ctx.font = 'bold 48px serif' // 字体大小设置
            ctx.strokeStyle = 'red'
            ctx.fillStyle = 'yellow'
            let text = 'Hello world'
            ctx.strokeText(text, 50, 100); // 绘制文本描边
            ctx.fillText(text, 50, 100); // 绘制文字
            ctx.measureText(text).width
            console.log('ctx.measureText.width: ', ctx.measureText(text).width); // 获取文本长度
        </script>
    </body>
</html>
